package com.whfc.fvs.service.impl;

import com.whfc.common.enums.DelFlag;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.fvs.dao.FvsDeviceMapper;
import com.whfc.fvs.dao.FvsRecordMapper;
import com.whfc.fvs.dto.FvsRecordDTO;
import com.whfc.fvs.entity.FvsDevice;
import com.whfc.fvs.entity.FvsRecord;
import com.whfc.fvs.service.FvsRecordService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @author: qzexing
 * @version: 1.0
 * @date: 2020/4/29 14:38
 */
@DubboService(interfaceClass = FvsRecordService.class, version = "1.0.0")
public class FvsRecordServiceImpl implements FvsRecordService {

    @Autowired
    private FvsRecordMapper fvsRecordMapper;

    @Autowired
    private FvsDeviceMapper fvsDeviceMapper;

    @Override
    public void insertRecord(FvsRecordDTO fvsRecordDTO) throws BizException {
        FvsRecord fvsRecord = new FvsRecord();
        BeanUtils.copyProperties(fvsRecordDTO, fvsRecord);
        Integer deviceId = fvsRecordDTO.getDeviceId();
        if (deviceId != null) {
            FvsDevice fvsDevice = fvsDeviceMapper.selectByPrimaryKey(deviceId);
            if (fvsDevice != null) {
                fvsRecord.setAliDeviceId(fvsDevice.getAliDeviceId());
                fvsRecord.setAliStreamId(fvsDevice.getAliStreamId());
            }
        }
        fvsRecord.setDelFlag(DelFlag.UNDELETE.getValue());
        fvsRecordMapper.insertSelective(fvsRecord);
    }

    @Override
    public ListData<FvsRecordDTO> getRecordList(Integer deviceId, Date startTime, Date endTime) throws BizException {
        ListData<FvsRecordDTO> listData = new PageData<>();
        List<FvsRecordDTO> recordList = fvsRecordMapper.selectByDeviceId(deviceId, startTime, endTime);
        listData.setList(recordList);
        return listData;
    }
}
